<template lang="jade">
div
  //- Loader
  h-loader-full(url='loader')
  m-title(icon='add',name='添加培训课程')
  table.single
    mf-row(name='课程类型')
      select(v-model='p.type',placeholder='请选择课程类别')
        option(value='', selected) 请选择类别
        option(v-for='(n,i) in type', :value='i') {{n}}

    mf-row(name='课程名称')
      select(v-model='p.name',placeholder='请选择课程类别')
        option(value='', selected, disabled) 请选择课程名称
        option(v-for='(n,i) in list', :value='n.name' v-if='n.type == p.type') {{n.name}}

    mf-row(name='课程名称',v-if="p.name == '新增课程名称'"): input(type = 'text',v-model='p.newname',placeholder='请填写课程名称')
    
    mf-row(name='开课时间'): input(type = 'datetime-local',v-model='p.class_starttime',placeholder='请填写开课时间')
    mf-row(name='报名结束时间'): input(type = 'datetime-local',v-model='p.apply_endtime',placeholder='请填写报名截止时间')
    
    mf-row(name='上课地址')
      input(type = 'text',v-model='p.address',placeholder='请填写上课地址')
    mf-row(name='联系电话')
      input(type = 'text',v-model='p.mobile',placeholder='请填写联系电话')

    mf-row(name='跳转链接',v-if='p.type === 1')
      input(type = 'text',v-model='p.url',placeholder='请填写跳转链接')
    mf-row(name='课程关键字',v-if='p.type === 0',msg='长度不超过15个字！')
      input(type = 'text',v-model='p.keyword',placeholder='请填写课程关键字')
    mf-row(name='课程小图',v-if='p.type === 0'): mf-upload(uploadid='img',amount='1',rule='cultivate',:data='p.smallimg',@upload_data='text1')
    mf-row(name='课程图片',v-if='p.type == 1 || p.type == 2'): mf-upload(uploadid='img',rule='cultivate',:data='p.img',@upload_data='text')
    mf-row(name='往期课程图片',v-if='p.type === 0'): mf-upload(uploadid='img1',amount='1',rule='cultivate',:data='p.tiveimg',@upload_data='text2')
    mf-row(name='课程内容',v-show='p.type == 1 || p.type == 2')
      textarea#editor.editor(style='width: 500px; height: 200px;', v-model='p.content') {{p.content}}
    mf-row(name='课程简介',v-show='p.type === 0')
      textarea#summary.editor(style='width: 500px; height: 200px;', v-model='p.summary') {{p.summary}}
    mf-row(name='课程简介',v-show='p.type === 0')
      textarea#merit.editor(style='width: 500px; height: 200px;', v-model='p.merit') {{p.merit}}
    mf-row
      button(@click='button') 添加
</template>

<script>
export default {
	data () {
		return {
      loader:{
				list: 'cultivate/listsOne'
			},
      list:[],
      type:['瑞安机电培训','职称考试','企业培训'],
      p:{
        name:'',
        class_starttime:'',
        apply_endtime:'',
        img:[],
        url:'',
        keyword:'',
        type:'',
        address:'',
        mobile:'',
        content:'',
        summary:'',
        merit:'',
        smallimg:[],
        newname:'',
        tiveimg:[],
      },
      img:[]
		}
	},
	methods: {
    loadAfter(res){
			this.list = res.list[0]
      this.list.unshift({id:0,name:'新增课程名称',type:0})
      this.list.unshift({id:0,name:'新增课程名称',type:2})
      
		},
    text(data){
			this.p.img = data
		},
    text1(data){
			this.p.smallimg = data
		},
    text2(data){
			this.p.tiveimg = data
		},  

    button(){
      editor.sync()
      summary.sync()
      merit.sync()
			this.p.content = this.$dom.id('editor').value
      this.p.summary = this.$dom.id('summary').value
      this.p.merit = this.$dom.id('merit').value
      if(this.p.name == '' && this.p.newname == ''){
        this.$msg.error('请填写课程名称!')
        return
      }
      if(this.p.class_starttime == ''){
        this.$msg.error('请选择开课时间!')
        return
      }
      if(this.p.apply_endtime == ''){
        this.$msg.error('请选择课程结束时间!')
        return
      }
      if(this.p.img.length == 0 && this.p.type != 0){
        this.$msg.error('请上传图片!')
        return
      }
      if(this.p.smallimg.length == 0 && this.p.type == 0){
        this.$msg.error('请上传小图片!')
        return
      }
      if(this.p.tiveimg.length == 0 && this.p.type == 0){
        this.$msg.error('请上传课程显示图片!')
        return
      }
      if(this.p.type != 0 && this.p.type != 1 && this.p.type != 2){
        this.$msg.error('请选择课程类型!')
        return
      }
      if(this.p.type == 0 && this.p.keyword == ''){
        this.$msg.error('请填写瑞安机电课程的课程关键字!')
        return
      }else if(this.p.keyword.length > 15){
        this.$msg.error('课程关键字的长度超出范围!')
        return
      }
      if(this.p.address == ''){
        this.$msg.error('请填写上课地址!')
        return
      }
      if(this.p.mobile == ''){
        this.$msg.error('请填写联系方式!')
        return
      }
      if(this.p.type != 0 && this.p.content == ''){
        this.$msg.error('请填写课程内容!')
        return
      }
      if(this.p.type == 0 && this.p.summary == ''){
        this.$msg.error('请填写瑞安机电课程的课程简介!')
        return
      }
      if(this.p.type == 0 && this.p.merit == ''){
        this.$msg.error('请填写瑞安机电课程的课程优势!')
        return
      }
      if(this.p.newname !== ''){
        this.p.name = this.p.newname
      }
      this.$ajax.post('cultivate/add',this.p,(res)=>{
        this.$msg.success(res.message)
        setTimeout("window.location.href='../cultivate/list.html'",1500)
      },(res)=>{
        this.$msg.error(res.message)
      })
    }
	}
}
</script>
<style>
	input#img_small{display:none;}
</style>